﻿using System;
using System.Reflection;
using UcAsp.Net.Interface;

namespace UcAsp.Net.BLL
{
    public class OpenApiBLL
    {
        public string Login(string param, string code)
        {
            Assembly obj = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + @"\bin\UcAsp.Net.OpenApi.Dll");
            Type type = obj.GetType("UcAsp.Net.OpenApi." + param + ".Api");
            Object o = Activator.CreateInstance(type);
            bool result = ((IOpenApi)o).OpenApi(code);
            return BLL.SysParamBLL.GetParamByName("LoginRedirect");
        }
        public string GetLoginUrl(string param)
        {
            Assembly obj = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + @"\bin\UcAsp.Net.OpenApi.Dll");
            Type type = obj.GetType("UcAsp.Net.OpenApi." + param + ".Api");
            Object o = Activator.CreateInstance(type);
            string result = ((IOpenApi)o).Login();
            return result;
        }
        public bool Write(int userid)
        {
            Model.UserInfo userinfo = new BLL.UserInfoBLL().GetUserInfoByID(userid);
            Assembly obj = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + @"\bin\UcAsp.Net.OpenApi.Dll");
            Type type = obj.GetType("UcAsp.Net.OpenApi." + userinfo.OpenName + ".Api");
            Object o = Activator.CreateInstance(type);
            bool result = ((IOpenApi)o).WriteWeibo(SysParamBLL.GetParamByName("RecommendWeiboContent"), SysParamBLL.GetParamByName("RecommendWeiboFile"), userinfo.SessionID);
            if (userinfo.WeiboTime == null)
            {
                userinfo.Point += int.Parse(BLL.SysParamBLL.GetParamByName("WeiboPoint"));
            }
            else
            {
                TimeSpan ts = DateTime.Now - DateTime.Parse(userinfo.WeiboTime.ToString());
                if (Math.Abs(ts.Days) > 0)
                {
                    userinfo.Point += int.Parse(BLL.SysParamBLL.GetParamByName("WeiboPoint"));
                }
            }
            userinfo.WeiboTime = DateTime.Now;
            new UcAsp.Net.BLL.UserInfoBLL().Update(userinfo);
            return result;
        }
    }
}